<?php
namespace Framework;

/**
 * Generates output suitable for Ajax usage. Not strictly the most useful class, 
 * but it's needed for some consistency in abstraction, and maybe it'll encourage
 * development of ClientWidgets. 
 */
class JSONOutputGenerator {
	private $widgets = array();
	private $dataObj = null;
	
	public function addWidget(ClientWidget $widget) {
		$this->widgets[] = $widget;
	}
	
	public function render() {
		$buffer = array();
		$widgetBuffer = array();
		foreach($this->widgets as $widget) {
			$data = $widget->getWidgetData();
			$type = $widget->getWidgetType();
			$widgetBuffer[] = array('data'=>$data, 'type'=>$type);
		}
		$buffer['widgets'] = $widgetBuffer;
		$buffer['data'] = $this->dataObj;
		header('Content-Type: application/json');
		print json_encode($buffer);
	}
	
	/**
	 * Set an arbitrary object to be rendered
	 */
	public function setData($dataObj) {
		$this->dataObj = $dataObj;
	}
}